package com.codechuan.demo.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.codechuan.demo.R;
import com.codechuan.demo.common.Constant;
import com.codechuan.demo.dto.SysSchedule;
import com.codechuan.demo.utils.DialogUtil;

import java.util.List;

/**
 * @Deprecated 行程适配器
 */
public class ScheduleSelectBaseAdapter extends BaseAdapter {
    private Context context;
    private List<SysSchedule> schedules;

    public ScheduleSelectBaseAdapter(Context context, List<SysSchedule> schedules) {
        this.context = context;
        this.schedules = schedules;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }


    @Override
    public int getCount() {
        return schedules.size();
    }

    @Override
    public Object getItem(int i) {
        return schedules.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ScheduleSelectBaseAdapter.ViewHolder viewHolder;
        if (view == null) {
            // 获取布局样式内容,根据布局***.xml 生成视图对象
            view = LayoutInflater.from(context).inflate(R.layout.item_schedule_select, null);
            viewHolder = new ScheduleSelectBaseAdapter.ViewHolder();
            viewHolder.ll_item = view.findViewById(R.id.schedule_select_ll_item);
            viewHolder.schedule_id = view.findViewById(R.id.select_schedule_id);
            viewHolder.startCityName = view.findViewById(R.id.select_startCityName);
            viewHolder.sonOrderNumber = view.findViewById(R.id.select_sonOrderNumber);
            viewHolder.button = view.findViewById(R.id.schedule_select_button);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ScheduleSelectBaseAdapter.ViewHolder) view.getTag();
        }

        //  blocksDescendants 阻止下面的控件获得焦点
        viewHolder.ll_item.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);

        SysSchedule sysSchedule = schedules.get(i);
        // 设置回显数据
        String schedulesString = "行程" + (i + 1) + ":" + maskString(sysSchedule.getDriverJourneyGuid());
        viewHolder.schedule_id.setText(schedulesString);

        // 设置起点和终点位置
        String addressString = sysSchedule.getStartCityName() + "至" + sysSchedule.getEndCityName();
        viewHolder.startCityName.setText(addressString);

        // 设置子订单数量
        String sonOrderString = "行程有:" + sysSchedule.getSonOrderNumber() + "个订单";
        viewHolder.sonOrderNumber.setText(sonOrderString);

        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (viewHolder.button.getText().toString().equals("加入刷单行程")) {
                    viewHolder.button.setText("退出刷单行程");
                    viewHolder.button.setBackgroundResource(R.drawable.rounded_tc_button);
                    Constant.xc_id.add(sysSchedule.getDriverJourneyGuid());
                } else {
                    viewHolder.button.setText("加入刷单行程");
                    viewHolder.button.setBackgroundResource(R.drawable.rounded_button);
                    for (int i1 = 0; i1 < Constant.xc_id.size(); i1++) {
                        if ( Constant.xc_id.get(i1).equals(sysSchedule.getDriverJourneyGuid())){
                            Constant.xc_id.remove(i1);
                        }
                    }

                }
            }
        });

        return view;
    }

    public static String maskString(String str) {
        if (str == null || str.length() <= 10) {
            return str;
        }
        return str.substring(0, 8) + "******";
    }

    public static final class ViewHolder {
        public LinearLayout ll_item;

        public TextView schedule_id;
        public TextView startCityName;
        public TextView sonOrderNumber;
        public Button button;

    }
}
